﻿using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;

#nullable disable

namespace RazorPages.Models
{
    public partial class SMDBContext : DbContext
    {
        public SMDBContext()
        {
        }

        public SMDBContext(DbContextOptions<SMDBContext> options)
            : base(options)
        {
        }

        public virtual DbSet<Admin> Admins { get; set; }
        public virtual DbSet<Attendance> Attendances { get; set; }
        public virtual DbSet<ScoreList> ScoreLists { get; set; }
        public virtual DbSet<Student> Students { get; set; }
        public virtual DbSet<StudentClass> StudentClasses { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!optionsBuilder.IsConfigured)
            {
              //#warning To protect potentially sensitive information in your connection string, you should move it out of source code. You can avoid scaffolding the connection string by using the Name= syntax to read it from configuration - see https://go.microsoft.com/fwlink/?linkid=2131148. For more guidance on storing connection strings, see http://go.microsoft.com/fwlink/?LinkId=723263.
                optionsBuilder.UseSqlServer("Server=WIN-UMRSB78SIKM;database=SMDB;Trusted_Connection=True;");
            }
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.HasAnnotation("Relational:Collation", "Chinese_PRC_CI_AS");

            modelBuilder.Entity<Admin>(entity =>
            {
                entity.HasKey(e => e.LoginId)
                    .HasName("PK__Admins__4DDA28183BC03C6A");

                entity.Property(e => e.AdminName)
                    .IsRequired()
                    .HasMaxLength(20)
                    .IsUnicode(false);

                entity.Property(e => e.LoginPwd)
                    .IsRequired()
                    .HasMaxLength(20)
                    .IsUnicode(false);
            });

            modelBuilder.Entity<Attendance>(entity =>
            {
                entity.ToTable("Attendance");

                entity.Property(e => e.CardNo)
                    .IsRequired()
                    .HasMaxLength(20)
                    .IsUnicode(false);

                entity.Property(e => e.Dtime)
                    .HasColumnType("smalldatetime")
                    .HasColumnName("DTime")
                    .HasDefaultValueSql("(getdate())");
            });

            modelBuilder.Entity<ScoreList>(entity =>
            {
                entity.ToTable("ScoreList");

                entity.Property(e => e.Csharp).HasColumnName("CSharp");

                entity.Property(e => e.SqlserverDb).HasColumnName("SQLServerDB");

                entity.Property(e => e.UpdateTime)
                    .HasColumnType("smalldatetime")
                    .HasDefaultValueSql("(getdate())");

                entity.HasOne(d => d.Student)
                    .WithMany(p => p.ScoreLists)
                    .HasForeignKey(d => d.StudentId)
                    .OnDelete(DeleteBehavior.ClientSetNull)
                    .HasConstraintName("fk_StudentId");
            });

            modelBuilder.Entity<Student>(entity =>
            {
                entity.HasIndex(e => e.CardNo, "uq_CardNo")
                    .IsUnique();

                entity.HasIndex(e => e.StudentIdNo, "uq_StudentIdNo")
                    .IsUnique();

                entity.Property(e => e.Birthday).HasColumnType("smalldatetime");

                entity.Property(e => e.CardNo)
                    .IsRequired()
                    .HasMaxLength(20)
                    .IsUnicode(false);

                entity.Property(e => e.Gender)
                    .IsRequired()
                    .HasMaxLength(2)
                    .IsUnicode(false)
                    .IsFixedLength(true);

                entity.Property(e => e.PhoneNumber)
                    .HasMaxLength(50)
                    .IsUnicode(false);

                entity.Property(e => e.StuImage).HasColumnType("text");

                entity.Property(e => e.StudentAddress)
                    .HasMaxLength(500)
                    .IsUnicode(false)
                    .HasDefaultValueSql("('地址不详')");

                entity.Property(e => e.StudentIdNo).HasColumnType("numeric(18, 0)");

                entity.Property(e => e.StudentName)
                    .IsRequired()
                    .HasMaxLength(20)
                    .IsUnicode(false);

                entity.HasOne(d => d.Class)
                    .WithMany(p => p.Students)
                    .HasForeignKey(d => d.ClassId)
                    .OnDelete(DeleteBehavior.ClientSetNull)
                    .HasConstraintName("fk_classId");
            });

            modelBuilder.Entity<StudentClass>(entity =>
            {
                entity.HasKey(e => e.ClassId)
                    .HasName("PK__StudentC__CB1927C0483126E2");

                entity.ToTable("StudentClass");

                entity.Property(e => e.ClassId).ValueGeneratedNever();

                entity.Property(e => e.ClassName)
                    .IsRequired()
                    .HasMaxLength(20)
                    .IsUnicode(false);
            });

            OnModelCreatingPartial(modelBuilder);
        }

        partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
    }
}
